package com.coship.vas.dapg.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;

import com.coship.vas.dapg.contans.PrgrelationContants;
import com.coship.vas.dapg.entity.Prgrelation;
import com.coship.vas.dapg.util.HttpPostUtils;
import com.coship.vas.dapg.util.XMLParser;

public class ProcessRelation {
    public static List<List<Element>> parserPrgrelation(Map<String, String> params,String callMethod) throws Exception {
        String prg_id = params.get(PrgrelationContants.REQUEST_PID); // 节目ID
        String desc_len = params.get(PrgrelationContants.REQUEST_DESC_LEN);// 简介长度
        String  imag_scale =  params.get(PrgrelationContants.REQUEST_IMAGE_SCALE);//图片比例
        String  imag_size =  params.get(PrgrelationContants.REQUEST_IMAGE_SIZE);//图片长度
        String  max_count =  params.get(PrgrelationContants.REQUEST_MAX_COUNT);//图片长度
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>");
        sb.append("<methodCall><methodName>").append(callMethod).append("</methodName>");
        sb.append("<params>");
        sb.append("<param><value><int>").append(prg_id).append("</int></value></param>");
        sb.append("<param><value><int>").append(desc_len).append("</int></value></param>");
        sb.append("<param><value><int>").append(imag_scale).append("</int></value></param>");
        sb.append("<param><value><int>").append(imag_size).append("</int></value></param>");
        sb.append("<param><value><int>").append(max_count).append("</int></value></param>");
        sb.append("</params></methodCall>");

        String response = HttpPostUtils.post(System.getProperty("system.http.post.url"), sb.toString(), "utf-8");
        Document document = XMLParser.getDocument(response);
        //处理返回错误信息
        String parentError =  "//methodResponse/fault/value/struct";
        List<List<Element>> errorList  = XMLParser.getElementList(document,parentError,"member");
        String errMsg="";
        for(List<Element> el:errorList){
            for (Element e : el) {
                Element errorMesgElement = e.element("value").element("string");
                if(errorMesgElement!=null){
                    errMsg =  errorMesgElement.getTextTrim();
                }
            }
        }
        List<List<Element>> el_list=null;
        if(errorList!=null && errorList.size()>0){
          throw  new Exception(errMsg);
        }else{
           el_list = XMLParser.getElementList(document,
                    "//methodResponse/params/param/value/array/data/value/struct", "member");
        }
        //处理成功
        return el_list;
    }

    public static List<Prgrelation> xmlToBean(Map<String, String> params,String callMethod) throws Exception  {
        List<Prgrelation> list = new ArrayList<Prgrelation>();
        List<List<Element>>  responseList = parserPrgrelation(params,callMethod);
       if(responseList==null){return list;}
        for (List<Element> elists : responseList) {
            Prgrelation prgVo = new Prgrelation();
            for (Element e : elists) {
                // member/name
                Element name = e.element("name");
                // value
                Element value = e.element("value").element("string");
                if (PrgrelationContants.PRG_NAME.equals(name.getTextTrim())) {
                    prgVo.setPrgName(value.getTextTrim());
                } else if (PrgrelationContants.PRG_IMAGE.equals(name.getTextTrim())) {
                    prgVo.setPrgImage(value.getTextTrim());
                } else if (PrgrelationContants.PRG_DESC.equals(name.getTextTrim())) {
                    prgVo.setPrgDesc(value.getTextTrim());
                } else if (PrgrelationContants.EPISODE_NUMBE.equals(name.getTextTrim())) {
                    prgVo.setEpisodeNumbe(value.getTextTrim());
                } else if (PrgrelationContants.EPISODE_ID.equals(name.getTextTrim())) {
                    prgVo.setEpisodeId(value.getTextTrim());
                } else if (PrgrelationContants.DURATION.equals(name.getTextTrim())) {
                    prgVo.setDuration(value.getTextTrim());
                }

            }
            list.add(prgVo);
        }
        return list;
    }
}
